package com.emm.yixun.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.AppPushService;
import com.emm.yixun.basic.service.NoticeTemporaryService;
import com.emm.yixun.common.model.NoticeTemporary;
import com.emm.yixun.common.model.dto.NoticeTemporaryDto;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.system.service.NoticeTemporaryServiceFacade;

@Service("noticeTemporaryServiceFacade")
@WebService(name = "com.emm.yixun.system.service.NoticeTemporaryServiceFacade")
public class NoticeTemporaryServiceFacadeImpl implements NoticeTemporaryServiceFacade {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private NoticeTemporaryService noticeTemporaryService;
	@Autowired
	private AppPushService appPushService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.system.service.NoticeTemporaryServiceFacade#selectNoticeTemporary()
	 */
	@Override
	public Response<Boolean> selectNoticeTemporary() {
		Response<Boolean> response = new Response<Boolean>();
		response.setSuccess(Boolean.FALSE);
		try {
			Map<String, String> extraMap = new HashMap<>();
			NoticeTemporaryDto dto = new NoticeTemporaryDto();
			dto.setOrders(" msg_order desc");
			// List<NoticeTemporary> list = noticeTemporaryService.selectListByDto(dto);
			Page<NoticeTemporary> page = noticeTemporaryService.getPageModel(dto, 0, 500);
			List<NoticeTemporary> list = page.getRows();
			logger.info("NoticeTemporaryJob start send list count = " + list.size());
			int successCount = 0;
			if (list != null && list.size() > 0) {
				for (NoticeTemporary noticeTemporary : list) {
					extraMap.put("action", noticeTemporary.getAction());
					extraMap.put("pushType", noticeTemporary.getPushType());
					extraMap.put("projectCode", noticeTemporary.getProjectCode());
					extraMap.put("projectName", noticeTemporary.getProjectName());
					if (StringUtils.isNotBlank(noticeTemporary.getBusinessId())) {
						extraMap.put("businessId", noticeTemporary.getBusinessId());
					}
					if (StringUtils.isNotBlank(noticeTemporary.getCustomerId())) {
						extraMap.put("customerId", noticeTemporary.getCustomerId());
					}
					if (StringUtils.isNotBlank(noticeTemporary.getTaskId())) {
						extraMap.put("taskId", noticeTemporary.getTaskId());
					}
					if (StringUtils.isNotBlank(noticeTemporary.getTempType())) {
						extraMap.put("tempType", noticeTemporary.getTempType());
					}
					if (noticeTemporary.getRemindCategoryId() != null) {
						extraMap.put("remindCategoryId", String.valueOf(noticeTemporary.getRemindCategoryId()));
					}
					if (StringUtils.isNotBlank(noticeTemporary.getRemindCategory())) {
						extraMap.put("remindCategory", noticeTemporary.getRemindCategory());
					}

					String f = appPushService.jobPushMessage(noticeTemporary.getReceiverId().toString(),
							noticeTemporary.getContent(), noticeTemporary.getTitle(), extraMap);
					if ("1".equals(f)) {
						successCount++;
						NoticeTemporaryDto noticeTemporaryDto = new NoticeTemporaryDto();
						noticeTemporaryDto.setId(noticeTemporary.getId());
						noticeTemporaryService.destroy(noticeTemporaryDto);
					} else if ("2".equals(f)) {
						logger.error("消息发送失败,消息Id:" + noticeTemporary.getId());
					} else if ("0".equals(f)) {
						logger.info("消息发送失败,userToken 为空,移动端未登陆");
					}
				}
				logger.info("send msg success count = " + successCount);
			}
			response.setSuccess(Boolean.TRUE);
		} catch (Exception e) {
			logger.error("send msg error", e);
			response.setResultMessage(e.getMessage());
		}
		return response;
	}

}
